//
//  BNDPushVCHandler.m
//  OfficeDoc
//
//  Created by pie on 2022/4/1.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDPushVCHandler.h"
#import "BNDOfficeDocDetailsVc.h"
#import "BNDPreference.h"

@implementation BNDPushVCHandler

+ (UINavigationController *)currentSelectedNavController
{
   
    id<UIApplicationDelegate> delegate = (id<UIApplicationDelegate>)[UIApplication sharedApplication].delegate;
    NSLog(@"%@",delegate);
    UIViewController *viewContoller = delegate.window.rootViewController;
    
    if (viewContoller != [self presentingViewController] && [[self presentingViewController] isKindOfClass:[UINavigationController class]]) {
        return (UINavigationController *)[self presentingViewController];
    }else if ([viewContoller isKindOfClass:[UINavigationController class]]) {
        return (UINavigationController *)viewContoller;
    }
    
    return nil;
}

+ (UIViewController *)presentingViewController
{
    id<UIApplicationDelegate> delegate = (id<UIApplicationDelegate>)[UIApplication sharedApplication].delegate;
    NSLog(@"%@",delegate);
    UIViewController *viewContoller = delegate.window.rootViewController;
    NSLog(@"%@",viewContoller);
//    if (!viewContoller.presentedViewController) {
//        if ([viewContoller isKindOfClass:[UINavigationController class]]) {
//            return [viewContoller presentedViewController];
//        }
//    }
    
    while (viewContoller.presentedViewController) {
        viewContoller = viewContoller.presentedViewController;
    }
    
    return viewContoller;
}

+ (UIViewController *)topViewController
{
    UINavigationController *nav = [self currentSelectedNavController];
    if([nav isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *rootVC = (UINavigationController *)nav;
        
//        NSLog(@"topViewController %@", rootVC.rt_topViewController);
        return rootVC.topViewController;
    }
    NSLog(@"topViewController %@", nav.topViewController);
    return nav.topViewController;
}

+ (void)pushOfficeDocDetailsVc:(BNDOfficeDocModel*)detailsID action:(VoidBlock)block
{
    BNDOfficeDocDetailsVc *vc = [[BNDOfficeDocDetailsVc alloc] init];
    vc.model = detailsID;
    vc.unReadStateBlock = block;
    [[BNDPushVCHandler currentSelectedNavController] pushViewController:vc animated:YES];
}

+ (void)pushWenVc:(NSString*)url withDownload:(BNDWebState)state  name:(NSString*)name
{
    BNDOfficeDocDetailsWebVc *vc = [[BNDOfficeDocDetailsWebVc alloc] init];
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    [info setObject:name forKey:@"name"];
    vc.title = name;
    vc.info = info;
    [vc loadDataMethod:url withState:state];
    [[BNDPushVCHandler currentSelectedNavController] pushViewController:vc animated:YES];
}
@end
